package UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

public class Menu extends JFrame{
    private ChangeUI changeUI;

    public void setChangeUI(ChangeUI changeUI) {
        this.changeUI = changeUI;
    }

    public Menu() {
        init();//创建对象时,调用页面初始化方法
    }
    /** 1.初始化界面组件和布局的 */
    private void init(){
        setTitle("指针信息在线测评");
        setSize(600, 400);
        //2.在初始化界面中添加创建内容窗格方法
        setContentPane(createContentPane());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                changeUI.exit();
            }
        });
    }

    /** 2.创建内容窗格*/
    private JPanel createContentPane() {
        JPanel pane = new JPanel(new BorderLayout());
        ImageIcon icon = new ImageIcon
                (this.getClass().getClassLoader().getResource("pic/title.png"));
        pane.add(BorderLayout.NORTH, new JLabel(icon));
        pane.add(BorderLayout.CENTER, createMenuPane());
        pane.add(BorderLayout.SOUTH, new JLabel
                ("指针信息--版权所有 盗版必究", JLabel.RIGHT));
        return pane;
    }

    /** 3.多个布局+图片*/
    private JLabel info;//info是私有成员变量
    private JPanel createMenuPane() {
        JPanel pane = new JPanel(new BorderLayout());
        //务必将info 引用到界面控件对象
        info = new JLabel("XXX 同学您好!", JLabel.CENTER);
        pane.add(BorderLayout.NORTH, info);
        pane.add(BorderLayout.CENTER, createBtnPane());
        return pane;
    }

    /*3.1 设置中部四个按钮,都是图片*/
    private JPanel createBtnPane() {
        JPanel pane = new JPanel();
        JButton start = createImgBtn("pic/exam.png", "开始");
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeUI.enterTest();
            }
        });
        JButton result = createImgBtn("pic/result.png", "分数");
        JButton msg = createImgBtn("pic/message.png", "考试规则");
        msg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               changeUI.showRules();
            }
        });
        JButton exit = createImgBtn("pic/exit.png", "离开");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeUI.exit();
            }
        });
        //将4个图片按钮添加到画板中
        pane.add(start);
        pane.add(result);
        pane.add(msg);
        pane.add(exit);
        return pane;
    }
    //3.1-1 创建图片按钮的方法
    private JButton createImgBtn(String img, String txt) {
        /*I:获取图片资源,但是最终的img图片是会变得
            所以不能是一张固定的图片
            最终获取的img名字,在外部获取(提供),然后进行匹配*/
        ImageIcon ico = new ImageIcon(this.getClass().getClassLoader().getResource(img));
        /*II:调用JButton的有参构造方法
         *   txt:按钮的文本,外部传进来的信息
         *   ico:显示在按钮上的Icon图像,外部传进来的信息
         */
        JButton button = new JButton(txt, ico);
        // 设置文本相对于图标的垂直位置(BOTTOM:底部)
        button.setVerticalTextPosition(JButton.BOTTOM);
        // 设置文本相对于图标的水平位置(CENTER:居中)
        button.setHorizontalTextPosition(JButton.CENTER);
        return button;
    }

    public void updateView(String name) {
        info.setText("欢迎"+name+"同学");
    }
}
